package TouristManagementBO.TouristOffice;

import org.omg.CosNaming.NamingContext;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import TouristManagement.*;

public class TouristOfficeServer {
	
	private ServerNamingTourist leServerNamingTourist;
	
	public void initServer(){
		this.connectToServerNamingTourist();
	}
	
	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void run(){
		try {			
	        // Intialisation de l'ORB
	        //************************
            
            String args[] = null;
            
	        org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Gestion du POA
	        //****************
	        // Recuperation du POA
	        POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

	        // Creation du servant
	        //*********************
	        TouristOfficeImpl leTouristOffice = new TouristOfficeImpl();
	        leTouristOffice.initServer();

	        // Activer le servant au sein du POA et recuperer son ID
	        rootPOA.activate_object(leTouristOffice);

	        // Activer le POA manager
	        rootPOA.the_POAManager().activate();
	
	        // Enregistrement dans le service de nommage
	        //*******************************************
	        // Recuperation du naming service
	        NamingContext nameRoot=org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a enregistrer
	        org.omg.CosNaming.NameComponent[] nameToRegister = new org.omg.CosNaming.NameComponent[1];
	        //enregister dans le Naming service
	        nameToRegister[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.nomTouristOffice(),"");

	        // Enregistrement de l'objet CORBA dans le service de noms
	        nameRoot.rebind(nameToRegister,rootPOA.servant_to_reference(leTouristOffice));

	        System.out.println("TouristOffice created");

	        // Lancement de l'ORB et mise en attente de requete
	        //**************************************************
	        orb.run();
	    }
		catch (Exception e) {
			e.printStackTrace();
		}
	} // main
	
	public static void main(String[] args) {
		TouristOfficeServer leTouristOfficeServer = new TouristOfficeServer();
		leTouristOfficeServer.initServer();
		leTouristOfficeServer.run();
		
	}
		
}
